/*
 * @FilePath: /data-and-structure-lv/10/stack2.c
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-12-26 10:42:02
 * @Author: zsk
 * @Copyright: open source
 * @LastEditors: zsk
 * @LastEditTime: 2021-12-26 14:25:04
 */
#include <stdio.h>

#define N 1024
typedef int ElementType;

typedef struct Stack_Tag
{
    ElementType s[N];
    int up_top;
    int down_top;
} Stack;

int stack_empty(Stack *s)
{
    if (s->down_top == -1 && s->up_top == N / 2 - 1)
        return 1;
    else
        return 0;
}

void push(Stack *s, ElementType x)
{
    if (s->down_top < N / 2)
    {
        /* code */
        s->down_top += 1;
        s->s[s->down_top] = x;
    }
    else
    {
        s->up_top += 1;
        s->s[s->up_top] = x;
    }
}

ElementType pop(Stack *s)
{
    if (stack_empty(s))
    {
        /* code */
        printf("underflow\n");
        return -1;
    }
    else if (s->up_top > N / 2 - 1)
    {
        s->up_top -= 1;
        return s->s[s->up_top + 1];
    }
    else
    {
        s->down_top -= 1;
        return s->s[s->down_top + 1];
    }
}

void main()
{
    Stack s = {.down_top = -1, .up_top = N / 2 - 1};
    Stack *sp = &s;
    for (int i = 0; i < N; i++)
    {
        push(sp, i);
    }

    for (int i = 0; i < N + 1; i++)
    {
        ElementType x = pop(sp);
        printf("%d ", x);
    }
    printf("\n");
}